<?
require_once(dirname(__FILE__) . '/../models/ShippingServices.php');
require_once(dirname(__FILE__) . '/../models/Countries.php');
require_once(dirname(__FILE__) . '/../models/Orders.php');
require_once(dirname(__FILE__) . '/../models/OrdersDetails.php');
require_once(dirname(__FILE__) . '/../models/OrdersStatusesChanges.php');
require_once('IndexController.php');

class CartController extends IndexController {

	function indexAction() {
		//Database::instance()->getConn()->debug=1;
		$this->view->referer = (isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'/');
		$auth = Zend_Auth::getInstance();
		if ($auth->hasIdentity()) {
			$userDetails = $auth->getIdentity();
			$this->view->userDetails = $userDetails;
		}

		$cartSession = new Zend_Session_Namespace('cart');
		$cartObjects = array();

		//Database::instance()->getConn()->debug=1;

		$i = 0;
		if (isset($cartSession->cart) && is_array($cartSession->cart))
		foreach ($cartSession->cart as $v) {

			$i++;
		}
		//debug($cartObjects);

		$countries = ModelMapper::instance('Countries')->findAll('', "ORDER BY country ASC");

		$this->view->countries = $countries;
		$this->view->cart = $cartObjects;
		$this->view->shippingService = ModelMapper::instance('ShippingServices')->find(1);
		$this->view->pageInfo = ModelMapper::instance('Pages')->find(49);
		
		$checkoutSession = new Zend_Session_Namespace('data');
		//debug($checkoutSession->data);
		if (is_array($checkoutSession->data) && !empty($checkoutSession->data))
			$checkoutSessionData = $checkoutSession->data;
		else 
			$checkoutSessionData = false;
		$this->view->formData = $checkoutSessionData;
		//debug($cartSession->cart);
	}

	function addAction() {
		//debug($_GET);
		
		$cartSession = new Zend_Session_Namespace('cart');
		if (!is_array($cartSession->cart))
			$cartSession->cart = array();
		
			
		
		$existsInCart = false;
		
		$smarty = $this->view->getEngine();

		

		$this->view->artwork = $artwork;
		$this->view->device = $device;

		$this->view->cartCount = sizeof($cartSession->cart);
		$tpl = $smarty->fetch('cart/add.tpl');

		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		//echo sizeof($cartSession->cart);
		exit();
	}

	function removeAction() {
		$cartSession = new Zend_Session_Namespace('cart');
		unset($cartSession->cart[intval($this->getRequest()->getParam('indexKey'))]);

		$cart = array();
		foreach ($cartSession->cart as $c) {
			$cart[] = $c;
		}
		$cartSession->cart = $cart;
		echo sizeof($cartSession->cart);
		exit();
	}
	
	function checkoutAction() {
		$smarty = $this->view->getEngine();
		
		$formData = $this->getRequest()->getParam('addForm');
		//debug($formData);
		
		$cartSession = new Zend_Session_Namespace('cart');
		foreach ($formData['quantity'] as $k => $q) {
			$cartSession->cart[$k]['quantity'] = $q;
		}
		
		$cartObjects = array();

		//Database::instance()->getConn()->debug=1;

		$i = 0;
		if (isset($cartSession->cart) && is_array($cartSession->cart))
		foreach ($cartSession->cart as $v) {
			$i++;
		}
		
		if (!isset($formData['useDifferentBAddr'])){
			$formData['bAddr'] = $formData['sAddr'];
			$formData['bAddr']['companyName'] = '';
			$formData['useDifferentBAddr'] = 0;
		}
		
		if (!isset($formData['paymentMethodId'])){
			$formData['paymentMethodId'] = 1;
		}
		
		$checkoutSession = new Zend_Session_Namespace('data');
		$checkoutSession->data = $formData;
		//debug($cartObjects);
		//debug($cartSession->cart);
		$this->view->cartObjects = $cartObjects;
		$this->view->formData = $checkoutSession->data;
		$this->view->shippingService = ModelMapper::instance('ShippingServices')->find(1);
		
		//$tpl = $smarty->fetch('cart/checkout.tpl');
		//$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		//$this->getResponse()->sendResponse();

		//exit();
	}
	
	function checkoutSaveAction() {
		$smarty = $this->view->getEngine();

		$cartSession = new Zend_Session_Namespace('cart');
		$checkoutSession = new Zend_Session_Namespace('data');
		
		$cartData = ($cartSession->cart);
		$orderData = ($checkoutSession->data);
		
		//Database::instance()->getConn()->debug=1;
		//$checkoutSession->data['saved'] = false;
		if (!isset($checkoutSession->data['saved']) || $checkoutSession->data['saved'] == false){
			Database::instance()->startTrans();
			
			$order = new Orders();
			$order->setUsersId($this->loggedUser != false?$this->loggedUser->getId():null);
			
			$order->setLastStatusesId($this->config->statuses->orders->received);
			$order->setShippingServicesId($orderData['shippingServicesId']);
			$order->setOrderDate(date('Y-m-d H:i:s'));
			$order->setSaddrFirstName($orderData['sAddr']['firstName']);
			$order->setSaddrLastName($orderData['sAddr']['lastName']);
			$order->setSaddrEmail($orderData['sAddr']['email']);
			$order->setSaddrStreet($orderData['sAddr']['street']);
			$order->setSaddrBuildingNo($orderData['sAddr']['buildingNo']);
			$order->setSaddrApartmentNo($orderData['sAddr']['apartmentNo']);
			$order->setSaddrPostcode($orderData['sAddr']['postcode']);
			$order->setSaddrCity($orderData['sAddr']['city']);
			$order->setSaddrCountry($orderData['sAddr']['country']);
			$order->setBaddrCompanyName($orderData['bAddr']['companyName']);
			$order->setBaddrFirstName($orderData['bAddr']['firstName']);
			$order->setBaddrLastName($orderData['bAddr']['lastName']);
			$order->setBaddrEmail($orderData['bAddr']['email']);
			$order->setBaddrStreet($orderData['bAddr']['street']);
			$order->setBaddrBuildingNo($orderData['bAddr']['buildingNo']);
			$order->setBaddrApartmentNo($orderData['bAddr']['apartmentNo']);
			$order->setBaddrPostcode($orderData['bAddr']['postcode']);
			$order->setBaddrCity($orderData['bAddr']['city']);
			$order->setBaddrCountry($orderData['bAddr']['country']);
			$order->setBaddrVatId('');
			$order->setRemarks('');
			$order->setSessionId(md5($this->view->sessionId.time()));
			
			ObjectWatcher::flushOperations();
			
			$orderStatus = new OrdersStatusesChanges();
			$orderStatus->setOrdersId($order->getId());
			$orderStatus->setStatusesId($this->config->statuses->orders->received);
			$orderStatus->setAddDate(date('Y-m-d H:i:s'));
			$orderStatus->setUsersId($this->loggedUser != false?$this->loggedUser->getId():null);
			$orderStatus->setRemarks('');
			
			
			Database::instance()->completeTrans();
			$checkoutSession->data = 0;
			$cartSession->cart = 0;
		}
		
		
		$this->view->orderGUID = $order->getSessionId();
		//$this->view->formData = $checkoutSession->data;
		//$this->view->shippingService = ModelMapper::instance('ShippingServices')->find(1);
		//exit();
	}
	
	function updateQuantityAction(){
		$cartSession = new Zend_Session_Namespace('cart');
		$cartSession->cart[intval($this->getRequest()->getParam('indexKey'))]['quantity'] = intval($this->getRequest()->getParam('quantity'));
		exit();
	}
	
	function showOrderAction(){
		//Database::instance()->getConn()->debug=1;
		
		$filter = "WHERE session_id = '".$this->getRequest()->getParam('sessionId')."'";
		$order = ModelMapper::instance('Orders')->findOneFiltered($filter);
		if (!is_null($order)){
			splitOrders($orders = array($order));
			
			$this->view->or = $order;
		} else {
			die ('Bad Gateway');
		}
	}
}
?>
